# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dwd_send_warning_detail_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dwd__dwd_send_warning_detail_dt = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_send_warning_detail_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='new_doris',
    pre_sql=f"""
	TRUNCATE TABLE jms_dwd.dwd_send_warning_detail_dt PARTITION (
		 p{{{{ execution_date | cst_ds_nodash }}}}
		,p{{{{ execution_date | date_add(-1 ) | cst_ds_nodash }}}}
		,p{{{{ execution_date | date_add(-2 ) | cst_ds_nodash }}}}
		,p{{{{ execution_date | date_add(-3 ) | cst_ds_nodash }}}}
		,p{{{{ execution_date | date_add(-4 ) | cst_ds_nodash }}}}
		,p{{{{ execution_date | date_add(-5 ) | cst_ds_nodash }}}}
		,p{{{{ execution_date | date_add(-6 ) | cst_ds_nodash }}}}
		,p{{{{ execution_date | date_add(-7 ) | cst_ds_nodash }}}}
	);
	LOAD LABEL jms_dwd.{label} (
	    DATA INFILE(
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | date_add(-7 ) | cst_ds }}}}/*",
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | date_add(-6 ) | cst_ds }}}}/*",
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | date_add(-5 ) | cst_ds }}}}/*",
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | date_add(-4 ) | cst_ds }}}}/*",
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | date_add(-3 ) | cst_ds }}}}/*",
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | date_add(-2 ) | cst_ds }}}}/*",
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | date_add(-1 ) | cst_ds }}}}/*",
	    	"hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_send_warning_detail_dt/dt={{{{ execution_date | cst_ds }}}}/*"
	    )
	    INTO TABLE dwd_send_warning_detail_dt
	    FORMAT AS 'PARQUET'(
	         waybill_no
            ,package_code
            ,reg_scan_time
            ,registr_scan_site_code
            ,registr_scan_site_name
            ,bagg_scan_site_code
            ,bagg_scan_site_name
            ,bagg_scan_time
            ,first_center_code
            ,first_center_name
            ,first_center_agent_code
            ,first_center_agent_name
            ,first_center_arr_shipment_no
            ,first_center_lines_name
            ,first_center_arrival_time
            ,first_center_send_time
            ,end_center_actual_arr_time
            ,end_center_arr_time
            ,order_source_code
            ,order_source_name
            ,if_arr
        )
        columns from path as (dt)
        set(
             waybill_no                    = waybill_no
            ,package_code                  = package_code
            ,reg_scan_time                 = reg_scan_time
            ,registr_scan_site_code        = registr_scan_site_code
            ,registr_scan_site_name        = registr_scan_site_name
            ,bagg_scan_site_code           = bagg_scan_site_code
            ,bagg_scan_site_name           = bagg_scan_site_name
            ,bagg_scan_time                = bagg_scan_time
            ,first_center_code             = first_center_code
            ,first_center_name             = first_center_name
            ,first_center_agent_code       = first_center_agent_code
            ,first_center_agent_name       = first_center_agent_name
            ,first_center_arr_shipment_no  = first_center_arr_shipment_no
            ,first_center_lines_name       = first_center_lines_name
            ,first_center_arrival_time     = first_center_arrival_time
            ,first_center_send_time        = first_center_send_time
            ,end_center_actual_arr_time    = end_center_actual_arr_time
            ,end_center_arr_time           = end_center_arr_time
            ,order_source_code             = order_source_code
            ,order_source_name             = order_source_name
            ,if_arr                        = if_arr
            ,dt                            = dt
        )
    ) WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120),
)

